package com.coolgor.axnut.service.wechat.qyh.impl;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dto.wechat.qyh.QYHAccessTokenExecution;
import com.coolgor.axnut.dto.wechat.qyh.QYHMessageExecution;
import com.coolgor.axnut.enums.wechat.qyh.QYHAccessTokenStateEnum;
import com.coolgor.axnut.enums.wechat.qyh.QYHMessageOperationStateEnum;
import com.coolgor.axnut.exceptions.wechat.qyh.QYHMessageOperationException;
import com.coolgor.axnut.service.wechat.qyh.QYHAccessTokenService;
import com.coolgor.axnut.service.wechat.qyh.QYHMessageService;

@Service
public class QYHMessageServiceImpl implements  QYHMessageService {
	
	@Autowired
	private QYHAccessTokenService qyhAccessTokenService;
	
	@Override
	@Transactional
	public QYHMessageExecution sendText(Long recordID, //我们系统中记录的ID，自动到系统中获取信息
										String touser,//成员ID列表（消息接收者，多个接收者用‘|’分隔，最多支持1000个）。特殊情况：指定为@all，
										String toparty,//部门ID列表，多个接收者用‘|’分隔，最多支持100个。当touser为@all时忽略本参数
										String totag,//标签ID列表，多个接收者用‘|’分隔，最多支持100个。当touser为@all时忽略本参数
										String text,
										Boolean safe) throws QYHMessageOperationException {		
		if (recordID==null||recordID<1) {
			return new QYHMessageExecution(QYHMessageOperationStateEnum.NULL);
		}
		QYHAccessTokenExecution qyhAccessTokenExe = qyhAccessTokenService.getQYHAccessToken(recordID);
		if ( qyhAccessTokenExe.getState() == QYHAccessTokenStateEnum.SUCCESS.getState() ) {
			String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
			url = url + qyhAccessTokenExe.getQyhAccessToken().getAccessToken();				
	        String param = "{";
	        if ((touser==null||"".equals(touser))
	        	&& (touser==null||"".equals(touser))
	        	&& (touser==null||"".equals(touser))) {
	        	param += "\"touser\":\"@all\",";
	        } else if ("@all".equals(touser)) {
	        	param += "\"touser\":\"@all\",";
	        } else {
	        	if (touser!=null && "".equals(touser)) param += "\"touser\":\""+touser+"\",";
	        	if (toparty!=null && "".equals(toparty)) param += "\"toparty\":\""+toparty+"\",";
	        	if (totag!=null && "".equals(totag)) param += "\"toparty\":\""+totag+"\",";
	        }
	        param += "\"msgtype\":\"text\",";
	        param += "\"agentid\":" + qyhAccessTokenExe.getQyhAccessToken().getAgentId() + "," ; //企业应用的id，这里注意，必须是整型。
	        param += "\"text\":{\"content\":\"" + text +"\"},";//消息内容，最长不超过2048个字节，超过将截断
	        if (safe) {
	        	param += "\"safe\":1"; //表示是否是保密消息，0表示否，1表示是，默认0
	        } else {
	        	param += "\"safe\":0"; //表示是否是保密消息，0表示否，1表示是，默认0
	        }
	        try {
				Document doc = Jsoup.connect(url)
				        .header("Content-Type","application/json")
				        .ignoreContentType(true)
				        .requestBody(param)
				        .post();
			} catch (IOException e) {
				e.printStackTrace();
				return new QYHMessageExecution(QYHMessageOperationStateEnum.INNER_ERROR);
			}
		} else {
			return new QYHMessageExecution(QYHMessageOperationStateEnum.NULL);
		}										
		return new QYHMessageExecution(QYHMessageOperationStateEnum.SUCCESS,text);		
	}
	
}